package com.thesis.controller;

import com.thesis.common.CommonEnum;
import com.thesis.common.DateUtils;
import com.thesis.common.MD5Util;
import com.thesis.common.ResponseBean;
import com.thesis.entity.Food;
import com.thesis.entity.Record;
import com.thesis.entity.SysUser;
import com.thesis.service.RecordService;
import com.thesis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Slf4j
@Controller
@RequestMapping("/wx/manage")
public class WxController {

    @Resource
    private UserService userService;

    @Resource
    private RecordService recordService;

    @Value("${com.jane.security.md5.key}")
    private String md5Key;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping("/checkLogin")
    @ResponseBody
    public ResponseBean checkLogin(@RequestBody SysUser user) {

        String mobile = user.getMobile();
        String password = user.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            log.debug("手机号或密码为空！");
            return new ResponseBean(false,CommonEnum.BAD_REQUEST);
        }

        //根据手机号查找用户
        SysUser userByMobile = userService.findByMobile(mobile);
        if(userByMobile==null){
            return new ResponseBean(false,CommonEnum.NO_USER_EXIST);
        }
        //判断密码是否正确
        if (!MD5Util.md5Verify(user.getPassword(), this.md5Key, userByMobile.getPassword())) {
            return new ResponseBean(false, CommonEnum.INVALID_PASSWORD);
        }
        //通过所有验证

        return new ResponseBean<>(true, userByMobile, CommonEnum.LOGIN_SUCCESS);
    }

    /**
     * 柱状图
     * @param userId
     * @return
     */
    @RequestMapping("/lineData")
    // Map the HTTP request to the method lineData()
    @ResponseBody
    // Enable the response to be JSON format
    public ResponseBean lineData(@RequestParam String userId) {
        // Call the service method to find all records related to the user
        List<Map<String, Object>> records = recordService.findAllByFood(userId);

        // Return a ResponseBean object containing the response data
        return new ResponseBean<>(true, records, CommonEnum.LOGIN_SUCCESS);
    }

    @RequestMapping("/barData")
    @ResponseBody
    public ResponseBean barData(@RequestParam String userId) {
        List<Map<String, Object>> records = recordService.findAllByTime(userId);
        List<String> xAxis = new ArrayList<>();
        List<Object> series = new ArrayList<>();
        records.forEach(map ->{
            xAxis.add(map.get("time").toString());
            series.add(map.get("total").toString());
        });
        Map<String, Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("series",series);
        return new ResponseBean<>(true, result, CommonEnum.LOGIN_SUCCESS);
    }

    @RequestMapping("/analysis")
    @ResponseBody
    public ResponseBean analysis(@RequestBody Record record) {
        if(null == record.getName() || "".equals(record.getName())){
            return new ResponseBean(false, "名称不能为空！", CommonEnum.BAD_REQUEST);
        }
        if(null == record.getTotal() || "".equals(record.getTotal())){
            return new ResponseBean(false, "摄入量不能为空！", CommonEnum.BAD_REQUEST);
        }
        List<Food> foods = recordService.findList(record);
        if(null == foods || foods.isEmpty()){
            return new ResponseBean(false, "食物查不到信息！", CommonEnum.BAD_REQUEST);
        }
        Food food = foods.get(0);
        Map<String, Object> ret = new HashMap<>();
        ret.put("per", food.getKll() + "千卡/克");
        ret.put("ke", record.getTotal() + "克");
        ret.put("total", food.getKll().multiply(record.getTotal()) + "千卡");
        return new ResponseBean<>(true, ret, CommonEnum.LOGIN_SUCCESS);
    }
}
